package com.aliyun.heiyu.powermaster.server;

import io.grpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by rayt on 10/6/16.
 */
public class ServerInterceptor implements io.grpc.ServerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(ServerInterceptor.class);

    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
        //从原数据获取ip
        String user_ip = metadata.get(Constant.X_FORWARDED_FOR_METADATA_KEY);
        //将元数据传入到Context中
        Context ctx = Context.current().withValue(Constant.USER_IP_KEY, user_ip);

        return Contexts.interceptCall(ctx, serverCall, metadata, serverCallHandler);
    }
    
} 